Type TCamera
	Const minZoom#=.01
	Global _list:TList=New TList
	Global activeCamera:TCamera
	
	Field _link:TLink
	Field _x#,_y#
	Field _width#,_height#
	Field _zoom#=1
	Field _angle#=0
	
	Function Create:TCamera(width#=800,height#=600)
		Local c:TCamera=New TCamera
		
		c.setActiveCamera()
		
		c._width=width
		c._height=height
		
		c._link=_list.AddLast(c)
		Return c
	End Function
	
	Method setActiveCamera()
		activeCamera=Self
		
		SetViewport(0,0,_width,_height)
		SetScale(1,1)
		
		SetLineWidth(1)
		
		SetAlpha(1)
		SetBlend(SOLIDBLEND)
		SetColor(255,255,255)
	End Method
	Function getActiveCamera:TCamera()
		Return activeCamera
	End Function
	
	Function debug()
		For Local c:TCamera=EachIn _list
			c.draw
		Next
	End Function
	Method draw()
		If (Self<>activeCamera)
			Local p#[4,2]
			p[0,0]=_x  p[0,1]=_y
			p[1,0]=_x  p[1,1]=_y
			p[2,0]=_x  p[2,1]=_y
			p[3,0]=_x  p[3,1]=_y
			
			For Local x%=-1 To 1 Step 2
				For Local y%=-1 To 1 Step 2
					Local ind%=Max(x,0)*2+Max(y,0)
					
					Local ang#=vecAngle(_width*.5/_zoom*x,_height*.5/_zoom*y) - _angle
					Local dst#=dist(_width*.5/_zoom*x,_height*.5/_zoom*y)
					
					p[ind,0]=p[ind,0]-angleX(ang)*dst
					p[ind,1]=p[ind,1]-angleY(ang)*dst
					
					projectXY(p[ind,0],p[ind,1])
				Next
			Next
			
			DrawLine p[0,0],p[0,1],p[1,0],p[1,1]
			DrawLine p[1,0],p[1,1],p[2,0],p[2,1]
			DrawLine p[2,0],p[2,1],p[3,0],p[3,1]
			DrawLine p[3,0],p[3,1],p[0,0],p[0,1]
			
			DrawLine p[0,0],p[0,1],p[2,0],p[2,1]
			DrawLine p[1,0],p[1,1],p[3,0],p[3,1]
		EndIf
	End Method
	
	Function render()
		If activeCamera._zoom>0 Flip
		SetClsColor 50,50,50
		Cls
	End Function
	
	Method setPosition(x#,y#)
		_x=x
		_y=y
	End Method
	Method move(x#,y#)
		Local vec#=vecAngle(x,y)
		Local dist#=dist(x,y)
		vec:-_angle
		_x:+angleX(vec)*dist
		_y:+angleY(vec)*dist
	End Method
	Method translate(x#,y#)
		_x:+x
		_y:+y
	End Method
	
	Method setZoom(zoom#)
		_zoom=zoom
		If _zoom<minZoom _zoom=minZoom
	End Method
	Method zoom(zoom#)
		_zoom:+zoom
		If _zoom<minZoom _zoom=minZoom
	End Method
	Method getZoom#()
		Return _zoom
	End Method
	
	Method setAngle(angle#)
		_angle=angle
	End Method
	Method turn(angle#)
		_angle:+angle
	End Method
	Method getAngle#()
		Return _angle
	End Method
	
	Function projectXY(x# Var,y# Var)
		x = (x-activeCamera._x)*activeCamera._zoom
		y = (y-activeCamera._y)*activeCamera._zoom
		Local dist#=dist(x,y)
		Local ang#=vecAngle(x,y)
		ang:+activeCamera._angle
		x = angleX(ang)*dist + (activeCamera._width*.5)
		y = angleY(ang)*dist + (activeCamera._height*.5)
	End Function
	
	Function pickXY(x# Var,y# Var)
		x=(x-activeCamera._width*.5)/activeCamera._zoom
		y=(y-activeCamera._height*.5)/activeCamera._zoom
		Local dist#=dist(x,y)
		Local ang#=vecAngle(x,y)
		ang:-activeCamera._angle
		x = angleX(ang)*dist
		y = angleY(ang)*dist
		x=x+activeCamera._x
		y=y+activeCamera._y
	End Function
	
	Function projectW#(width#)
		Return width*activeCamera._zoom
	End Function
	Function projectH#(height#)
		Return height*activeCamera._zoom
	End Function
	
	Function projectR#(angle#=0)
		Return angle+activeCamera._angle
	End Function
End Type